package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserPotentialData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class UserPotentialDaoImpl extends TempDataObjectDao<UserPotentialData> {
    public static String getUserPotentialDataSql = "select * from `t_u_potential` where `userId`=?";
    public UserPotentialData getUserPotentialData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserPotentialDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserPotentialDataSql = "update `t_u_potential` set potential=?,lastLoadTime=? where userId=?";
    @Override
    protected boolean updateData(UserPotentialData userPotentialData) {
        userPotentialData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userPotentialData.getPotential()));
        para.put(i++, new DbParameter(Types.BIGINT, userPotentialData.getLastLoadTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userPotentialData.getUserId()));
        if (getDb().executeNoneQuery(updateUserPotentialDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserPotentialDataSql = "insert into `t_u_potential`(userId,potential,lastLoadTime) values (?,?,?)";
    @Override
    protected boolean addData(UserPotentialData userPotentialData) {
        userPotentialData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userPotentialData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userPotentialData.getPotential()));
        para.put(i++, new DbParameter(Types.BIGINT, userPotentialData.getLastLoadTime()));
        if (getDb().executeNoneQuery(insertUserPotentialDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserPotentialData parseData(ResultSet rs) throws SQLException {
        UserPotentialData userPotentialData = new UserPotentialData();
        userPotentialData.setUserId(rs.getLong("userId"));
        userPotentialData.setPotential(rs.getInt("potential"));
        userPotentialData.setLastLoadTime(rs.getLong("lastLoadTime"));

        return userPotentialData;
    }
}
